#!/usr/bin/env python
# --*-- coding:utf-8 --*--
# author:g-y-b time:2020/6/8


import matplotlib.pyplot as plt
from skimage import io
import math
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


# def transfer_data(image_path):
#     img = io.imread(image_path)
#     points = []     # 存放图片中白色区域的点信息
#     bound = []      # 存放图片中白色区域的边界点信息
#     for x in range(img.shape[0]):
#         for y in range(img.shape[1]):
#             if img[x, y] == 255:
#                 points.append([x, y])  # 白色区域点
#                 # 判断是否为白色区域的边界
#                 if img[x - 1, y] == 0 or img[x + 1, y] == 0 or img[x, y - 1] == 0 or img[x, y + 1] == 0:
#                     bound.append([x, y])  # 白色区域边界
#     return points, bound
#
#
# def get_center_radius(image_path):
#     points, bound = transfer_data(image_path)
#     radius_list = []
#     for point in points:
#         min_radius = 1000
#         for bound_point in bound:
#             if min_radius <= 0:
#                 min_radius = 0
#                 break
#             # 求白色区域点到白色区域边界的最小距离
#             distance = math.sqrt((bound_point[0] - point[0]) ** 2
#                                  + (bound_point[1] - point[1]) ** 2)
#             if distance < min_radius:
#                 min_radius = distance
#
#         radius_list.append([point[0], point[1], min_radius])
#     max_radius = [0, 0, 0]
#     # 在所有阴影点离边界的最小距离中求取最大距离，即为内圆半径
#     for ele in radius_list:
#         if ele[2] > max_radius[2]:
#             max_radius = ele
#     return max_radius
#
#
# def parse_images():
#     center_radius = []
#     for i in range(1, 100):
#         if i < 10:
#             image_path = 'images/0' + str(i) + '.bmp'
#         else:
#             image_path = 'images/' + str(i) + '.bmp'
#         temp = get_center_radius(image_path)
#         center_radius.append({'index': i, 'data': temp})
#         print(image_path, temp)
#     return center_radius
#
#
# # 求出了99个离散的中心点
# center_radius = parse_images()
# print(center_radius)

center_radius = [{'index': 1, 'data': [256, 417, 29.832867780352597]}, {'index': 2, 'data': [256, 417, 30.0]}, {'index': 3, 'data': [256, 417, 30.0]},
                 {'index': 4, 'data': [256, 417, 30.0]}, {'index': 5, 'data': [256, 417, 30.0]}, {'index': 6, 'data': [257, 417, 30.01666203960727]},
                 {'index': 7, 'data': [257, 417, 30.01666203960727]}, {'index': 8, 'data': [258, 417, 30.01666203960727]}, {'index': 9, 'data': [257, 417, 30.0]},
                 {'index': 10, 'data': [257, 417, 30.0]}, {'index': 11, 'data': [258, 417, 29.966648127543394]}, {'index': 12, 'data': [258, 417, 29.614185789921695]},
                 {'index': 13, 'data': [258, 417, 29.410882339705484]}, {'index': 14, 'data': [261, 416, 29.410882339705484]},
                 {'index': 15, 'data': [262, 416, 29.410882339705484]}, {'index': 16, 'data': [262, 416, 29.274562336608895]},
                 {'index': 17, 'data': [265, 415, 29.274562336608895]}, {'index': 18, 'data': [265, 415, 29.274562336608895]},
                 {'index': 19, 'data': [266, 415, 29.154759474226502]}, {'index': 20, 'data': [266, 415, 29.068883707497267]},
                 {'index': 21, 'data': [271, 414, 29.017236257093817]}, {'index': 22, 'data': [272, 415, 29.017236257093817]},
                 {'index': 23, 'data': [274, 415, 29.017236257093817]}, {'index': 24, 'data': [275, 415, 29.017236257093817]},
                 {'index': 25, 'data': [277, 415, 29.017236257093817]}, {'index': 26, 'data': [279, 415, 29.017236257093817]},
                 {'index': 27, 'data': [281, 415, 29.017236257093817]}, {'index': 28, 'data': [293, 414, 29.068883707497267]},
                 {'index': 29, 'data': [293, 414, 29.154759474226502]}, {'index': 30, 'data': [292, 414, 29.154759474226502]},
                 {'index': 31, 'data': [299, 413, 29.274562336608895]}, {'index': 32, 'data': [303, 412, 29.410882339705484]},
                 {'index': 33, 'data': [303, 412, 29.410882339705484]}, {'index': 34, 'data': [303, 412, 29.410882339705484]},
                 {'index': 35, 'data': [303, 412, 29.410882339705484]}, {'index': 36, 'data': [303, 412, 29.410882339705484]},
                 {'index': 37, 'data': [303, 412, 29.410882339705484]}, {'index': 38, 'data': [315, 408, 29.5296461204668]},
                 {'index': 39, 'data': [310, 410, 29.410882339705484]}, {'index': 40, 'data': [324, 404, 29.5296461204668]},
                 {'index': 41, 'data': [324, 404, 29.5296461204668]}, {'index': 42, 'data': [332, 400, 29.5296461204668]},
                 {'index': 43, 'data': [322, 405, 29.410882339705484]}, {'index': 44, 'data': [354, 385, 29.698484809834994]},
                 {'index': 45, 'data': [354, 385, 29.698484809834994]}, {'index': 46, 'data': [354, 385, 29.732137494637012]},
                 {'index': 47, 'data': [354, 385, 29.698484809834994]}, {'index': 48, 'data': [354, 385, 29.698484809834994]},
                 {'index': 49, 'data': [354, 385, 29.698484809834994]}, {'index': 50, 'data': [354, 385, 29.698484809834994]},
                 {'index': 51, 'data': [364, 376, 29.698484809834994]}, {'index': 52, 'data': [365, 375, 29.698484809834994]},
                 {'index': 53, 'data': [372, 368, 29.68164415931166]}, {'index': 54, 'data': [379, 360, 29.68164415931166]},
                 {'index': 55, 'data': [380, 359, 29.68164415931166]}, {'index': 56, 'data': [399, 330, 29.410882339705484]},
                 {'index': 57, 'data': [386, 351, 29.154759474226502]}, {'index': 58, 'data': [390, 345, 29.154759474226502]},
                 {'index': 59, 'data': [398, 332, 29.154759474226502]}, {'index': 60, 'data': [411, 301, 29.410882339705484]}, {'index': 61, 'data': [411, 301, 29.410882339705484]}, {'index': 62, 'data': [406, 315, 29.154759474226502]}, {'index': 63, 'data': [411, 301, 29.154759474226502]}, {'index': 64, 'data': [408, 310, 29.120439557122072]}, {'index': 65, 'data': [414, 291, 29.120439557122072]}, {'index': 66, 'data': [416, 283, 29.120439557122072]}, {'index': 67, 'data': [418, 273, 29.017236257093817]}, {'index': 68, 'data': [418, 216, 29.120439557122072]}, {'index': 69, 'data': [418, 216, 29.120439557122072]}, {'index': 70, 'data': [413, 193, 29.154759474226502]}, {'index': 71, 'data': [413, 193, 29.154759474226502]}, {'index': 72, 'data': [413, 193, 29.154759474226502]}, {'index': 73, 'data': [413, 193, 29.410882339705484]}, {'index': 74, 'data': [413, 193, 29.410882339705484]}, {'index': 75, 'data': [406, 173, 29.154759474226502]}, {'index': 76, 'data': [391, 145, 29.206163733020468]}, {'index': 77, 'data': [391, 145, 29.410882339705484]}, {'index': 78, 'data': [395, 151, 29.154759474226502]}, {'index': 79, 'data': [380, 130, 29.206163733020468]}, {'index': 80, 'data': [375, 124, 29.206163733020468]}, {'index': 81, 'data': [369, 118, 29.206163733020468]}, {'index': 82, 'data': [380, 130, 29.410882339705484]}, {'index': 83, 'data': [372, 121, 29.698484809834994]}, {'index': 84, 'data': [371, 120, 29.698484809834994]}, {'index': 85, 'data': [372, 121, 29.698484809834994]}, {'index': 86, 'data': [357, 106, 29.410882339705484]}, {'index': 87, 'data': [352, 102, 29.206163733020468]}, {'index': 88, 'data': [357, 106, 29.206163733020468]}, {'index': 89, 'data': [357, 106, 29.206163733020468]}, {'index': 90, 'data': [352, 102, 29.410882339705484]}, {'index': 91, 'data': [317, 81, 29.410882339705484]}, {'index': 92, 'data': [323, 84, 29.410882339705484]}, {'index': 93, 'data': [310, 78, 29.154759474226502]}, {'index': 94, 'data': [301, 75, 29.154759474226502]}, {'index': 95, 'data': [301, 75, 29.154759474226502]}, {'index': 96, 'data': [301, 75, 29.154759474226502]}, {'index': 97, 'data': [275, 68, 29.120439557122072]}, {'index': 98, 'data': [275, 68, 29.120439557122072]}, {'index': 99, 'data': [275, 68, 29.120439557122072]}]

z_index = []
x_index = []
y_index = []
r_list = []
for item in center_radius:
    z_index.append(item['index'])
    x_index.append(item['data'][0])
    y_index.append(item['data'][1])
    r_list.append(item['data'][2])
radius = sum(r_list) / len(r_list)

fig = plt.figure()
for i in range(1, 13):
    # 采用多项式进行拟合x(z)与y(z)
    z_array = np.array(z_index)
    x_array = np.array(x_index)
    y_array = np.array(y_index)

    x_z_p = np.polyfit(z_array, x_array, deg=i)  # 用i次多项式拟合
    y_z_p = np.polyfit(z_array, y_array, deg=i)  # 用i次多项式拟合
    x_z_func = np.poly1d(x_z_p)
    y_z_func = np.poly1d(y_z_p)

    ax = fig.add_subplot(3, 4, i, projection='3d')
    # ax.plot(x_array, y_array, z_array)  # 画三维曲线
    ax.plot(y_array, x_array, z_array)  # 画三维曲线

    s = 'deg:' + str(i)
    ax.text2D(0.05, 0.95, s, transform=ax.transAxes)

    z_array = np.arange(0, 100, 0.01)
    xvals = x_z_func(z_array)
    yvals = y_z_func(z_array)
    # ax.plot(xvals, yvals, z_array, 'r.', markersize=radius)  # 画三维曲线
    ax.plot(yvals, xvals, z_array, 'r.', markersize=radius/12)  # 画三维曲线
plt.show()